<?php

namespace app\admin\controller\analysis;

use app\common\controller\Backend;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Sum;

class Analysis extends Backend
{
    protected $model = null;
    //关联查询的意思
    protected $relationSearch = true;
    public function __construct()
    {
        parent::__construct();
        $this->model = model("Business.Business");
        $this->Ordermodel = model("Subject.Order");

    }
    public function index()
    {
        // $sql = "SELECT COUNT(*) as c, b.*,sum(total) FROM fa_business_record AS r LEFT JOIN fa_business AS b ON r.busid=b.id GROUP BY r.busid ORDER BY r.busid";
        // $result = $this->model->order('id','asc')->select();
        // halt(collection($result)->toArray());
        // exit;
        if ($this->request->isAjax()) {
            $action = $this->request->param('action');
            if ($action == 'source') {
                $sql = "SELECT COUNT(*) AS c,s.`name` as sourcename FROM fa_business AS b LEFT JOIN fa_business_source AS s ON b.sourceid=s.id WHERE b.`deletetime` is null GROUP BY b.sourceid ORDER BY b.sourceid asc";
                $result = $this->model::query($sql);
                $x = array_column($result, 'sourcename');
                $y = array_column($result, 'c');
                $data = [
                    'x' => $x,
                    'y' => $y,
                ];
                return json($data);
            }
            if ($action == 'record') {
                $sql = "SELECT COUNT(*) as c, b.*,sum(total) FROM fa_business_record AS r LEFT JOIN fa_business AS b ON r.busid=b.id WHERE b.`deletetime` is null GROUP BY r.busid ORDER BY b.id asc";
                $result = $this->model::query($sql);
                $res = $this->model->order('id', 'asc')->select();
                $sum = array_column($result, 'sum(total)');
                // 去掉'-'字符
                $sum = array_map(function ($val) {
                    return str_replace('-', '', $val);
                }, $sum);
                $count = array_column($result, 'c');
                $name = array_column($res, 'nickname_text');
                $data = [
                    'name' => $name,
                    'count' => $count,
                    'sum' => $sum,
                ];
                return json($data);
            }
            if ($action == 'visit') {
                $sql = "SELECT COUNT(*) as c FROM fa_business_visit AS v LEFT JOIN fa_business AS b ON v.busid=b.id WHERE b.`deletetime` is null GROUP BY v.busid ORDER BY b.id asc;";
                $result = $this->model::query($sql);
                $res = $this->model->order('id', 'asc')->select();
                $count = array_column($result, 'c');
                $name = array_column($res, 'nickname_text');
                $data = [
                    'name' => $name,
                    'count' => $count,
                ];
                return json($data);
            }
            if ($action == 'deal') {
                $count = $this->model->count();
                $deal = $this->model->where('deal', 1)->count();
                $Nodeal = $this->model->where('deal', 0)->count();
                $data = [
                    'count' => $count,
                    'deal' => $deal,
                    'Nodeal' => $Nodeal,
                ];
                return json($data);
            }
            if ($action == 'createtime') {
                $sql = "SELECT COUNT(*) as c,createtime FROM fa_business WHERE deletetime is null GROUP BY createtime ORDER BY createtime ASC;";
                $result = $this->model::query($sql);
                $value = array_column($result, 'c');
                $createtime = array_column($result, 'createtime');
                $Ctime = [];
                foreach ($createtime as $item) {
                    $time = date("Y-m-d", $item);
                    array_push($Ctime, $time);
                }

                $data = [
                    'value' => $value,
                    'name' => $Ctime,
                    // 'Ocount'=>$Ocount
                ];
                return json($data);
            }
            if ($action == 'ordertime') {
                $date=$this->Ordermodel->column('createtime');
                $Otime=[];
                foreach ($date as $item) {
                    $time = date("Y-m-d", $item);
                    array_push($Otime, $time);
                }
                $num=array_count_values($Otime);
                $Oname=array_keys($num);
                $Odate=array_values($num);
                $data = [
                    'Odate'=>$Odate,
                    'Oname'=>$Oname,
                ];
                return json($data);
            }
        }

        return $this->view->fetch();
    }
}